package ibm.websphere.monitor.action;

import ibm.websphere.monitor.bean.JvmInfo;
import ibm.websphere.monitor.bean.MonitorInfo;
import ibm.websphere.monitor.bean.ThreadInfo;
import ibm.websphere.monitor.common.SpringContext;
import ibm.websphere.monitor.service.IJvmService;
import ibm.websphere.monitor.service.IThreadService;
import ibm.websphere.monitor.util.Constants;
import ibm.websphere.monitor.util.TimeTool;
import ibm.websphere.monitor.util.UtilTool;

import java.util.List;

/**
 * <pre>
 *	@Description 处理监控所有ACTION//
 * </pre>
 * @copyright www.timotai.net
 * @author 这家伙真懒
 * @date 2016-1-12
 **/

public class MonitorManagerAction extends BaseAction {



    /**初始化IJvmService对象**/
    private IJvmService jvmService = SpringContext.getBean(IJvmService.class);

    /**初始化IThreadService对象**/
    private IThreadService threadService = SpringContext.getBean(IThreadService.class);

    /**server名**/
    private String server;

    /**查询时间**/
    private String currentDate = TimeTool.getNowTime();

    private List<Object> jvmList =null;

    private List<Object> threadList =null;

    private String   monitorJvm;

    private String   monitorThread;


    /**0:APP  1:WEB**/
    private Integer type = new Integer(0);


    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getCurrentDate() {
        return currentDate;
    }

    public void setCurrentDate(String currentDate) {
        this.currentDate = currentDate;
    }

    public String  getMonitorJvm() {
        return monitorJvm;
    }

    public void setMonitorJvm(String  monitorJvm) {
        this.monitorJvm = monitorJvm;
    }

    public String  getMonitorThread() {
        return monitorThread;
    }

    public void setMonitorThread(String  monitorThread) {
        this.monitorThread = monitorThread;
    }

    public List<Object> getJvmList() {
        return jvmList;
    }

    public void setJvmList(List<Object> jvmList) {
        this.jvmList = jvmList;
    }

    public MonitorInfo getMonitorInfo() {
        return monitorInfo;
    }

    public void setMonitorInfo(MonitorInfo monitorInfo) {
        this.monitorInfo = monitorInfo;
    }

    private MonitorInfo monitorInfo = new MonitorInfo();

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    /**
     *
     * @return
     */
    public String monitorIn(){


        server = this.getParam("serverName");
        System.out.println(this.getParam("type"));
        type = Integer.parseInt(this.getParam("type"));
        currentDate = this.getParam("currentDate");
        if(!UtilTool.checkStringValueExist(currentDate)){
            currentDate = TimeTool.getNowTime();
        }

        this.setMonitorJvm(Constants.JVM_MONITOR);
        this.setMonitorThread(Constants.THREAD_MONITOR);

        jvmList = jvmService.findJvm(currentDate, server);

        threadList = threadService.findThread(currentDate, server,type);

        this.setTarget(Constants.WAS_MONITOR_PAGES_ROOT + "monitorInfo.jsp");

        StringBuffer monitorDate =new StringBuffer();
        for(Object object : jvmList){
            JvmInfo jvmInfo = (JvmInfo)object;

            monitorInfo.getCurrentJvmDate().append("'"+jvmInfo.getCurrentDate()+"',");
            monitorInfo.getFreeMemory().append("'"+jvmInfo.getFreeMemory()+"',");
            monitorInfo.getHeapSize().append("'"+jvmInfo.getHeapSize()+"',");
            monitorInfo.getProcessCpuUsage().append("'" + jvmInfo.getProcessCpuUsage()+"',");
            monitorInfo.getUsedMemory().append("'" + jvmInfo.getUsedMemory() + "',");
        }

        for(Object object : threadList){
            ThreadInfo threadInfo = (ThreadInfo)object;
            monitorInfo.getPoolSize().append("'"+threadInfo.getPoolSize()+"',");
            monitorInfo.getActiveCount().append("'"+threadInfo.getActiveCount()+"',");
            monitorInfo.getCreateCount().append("'"+threadInfo.getCreateCount()+"',");
            monitorInfo.getDestroyCount().append("'"+threadInfo.getDestroyCount()+"',");
        }

        return Constants.DEFAULT_VIEW;
    }

    public static void main(String args[]){
    }
}
